//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package ai_provider_admin_api_serv

import (
	"context"
	"time"

	"gitcode.com/eteam/api-server/dao"
	"gitcode.com/eteam/api-server/dao/config_dao"
	"gitcode.com/eteam/proto-gen-go.git/ai_provider_api"
	"go.mongodb.org/mongo-driver/mongo"
)

type AiProviderAdminApiImpl struct {
	ai_provider_api.UnimplementedAiProviderAdminApiServer
}

func (apiImpl *AiProviderAdminApiImpl) AddLlmProvider(ctx context.Context, req *ai_provider_api.AdminAddLlmProviderRequest) (*ai_provider_api.AdminAddLlmProviderResponse, error) {
	sess, ok := dao.CacheDao.GetAdminSession(ctx, req.AdminSessionId)
	if !ok {
		return &ai_provider_api.AdminAddLlmProviderResponse{
			Code:   ai_provider_api.AdminAddLlmProviderResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	if !sess.AuthOk {
		return &ai_provider_api.AdminAddLlmProviderResponse{
			Code:   ai_provider_api.AdminAddLlmProviderResponse_CODE_NOT_AUTH,
			ErrMsg: "未授权会话",
		}, nil
	}

	exist, err := config_dao.LlmProviderDao.Exist(ctx, req.ProviderInfo.ProviderId)
	if err != nil {
		return nil, err
	}
	if exist {
		return &ai_provider_api.AdminAddLlmProviderResponse{
			Code:   ai_provider_api.AdminAddLlmProviderResponse_CODE_PROVIDER_EXIST,
			ErrMsg: "供应商信息已存在",
		}, nil
	}

	nowTime := time.Now().UnixMilli()

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = config_dao.LlmProviderDao.Insert(sessCtx, &config_dao.LlmProviderInfoDbItem{
			ProviderId:  req.ProviderInfo.ProviderId,
			Name:        req.ProviderInfo.Name,
			NeedApiKey:  req.ProviderInfo.NeedApiKey,
			DefaultAddr: req.ProviderInfo.DefaultAddr,
			TimeStamp:   nowTime,
		})
		if err != nil {
			return nil, err
		}
		return &ai_provider_api.AdminAddLlmProviderResponse{
			Code: ai_provider_api.AdminAddLlmProviderResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*ai_provider_api.AdminAddLlmProviderResponse), nil
}

func (apiImpl *AiProviderAdminApiImpl) UpdateLlmProvider(ctx context.Context, req *ai_provider_api.AdminUpdateLlmProviderRequest) (*ai_provider_api.AdminUpdateLlmProviderResponse, error) {
	sess, ok := dao.CacheDao.GetAdminSession(ctx, req.AdminSessionId)
	if !ok {
		return &ai_provider_api.AdminUpdateLlmProviderResponse{
			Code:   ai_provider_api.AdminUpdateLlmProviderResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	if !sess.AuthOk {
		return &ai_provider_api.AdminUpdateLlmProviderResponse{
			Code:   ai_provider_api.AdminUpdateLlmProviderResponse_CODE_NOT_AUTH,
			ErrMsg: "未授权会话",
		}, nil
	}

	exist, err := config_dao.LlmProviderDao.Exist(ctx, req.ProviderInfo.ProviderId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &ai_provider_api.AdminUpdateLlmProviderResponse{
			Code:   ai_provider_api.AdminUpdateLlmProviderResponse_CODE_NO_PROVIDER,
			ErrMsg: "供应商信息不存在",
		}, nil
	}

	nowTime := time.Now().UnixMilli()

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = config_dao.LlmProviderDao.Update(sessCtx, &config_dao.LlmProviderInfoDbItem{
			ProviderId:  req.ProviderInfo.ProviderId,
			Name:        req.ProviderInfo.Name,
			NeedApiKey:  req.ProviderInfo.NeedApiKey,
			DefaultAddr: req.ProviderInfo.DefaultAddr,
			TimeStamp:   nowTime,
		})
		if err != nil {
			return nil, err
		}
		return &ai_provider_api.AdminUpdateLlmProviderResponse{
			Code: ai_provider_api.AdminUpdateLlmProviderResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*ai_provider_api.AdminUpdateLlmProviderResponse), nil
}

func (apiImpl *AiProviderAdminApiImpl) RemoveLlmProvider(ctx context.Context, req *ai_provider_api.AdminRemoveLlmProviderRequest) (*ai_provider_api.AdminRemoveLlmProviderResponse, error) {
	sess, ok := dao.CacheDao.GetAdminSession(ctx, req.AdminSessionId)
	if !ok {
		return &ai_provider_api.AdminRemoveLlmProviderResponse{
			Code:   ai_provider_api.AdminRemoveLlmProviderResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	if !sess.AuthOk {
		return &ai_provider_api.AdminRemoveLlmProviderResponse{
			Code:   ai_provider_api.AdminRemoveLlmProviderResponse_CODE_NOT_AUTH,
			ErrMsg: "未授权会话",
		}, nil
	}

	exist, err := config_dao.LlmProviderDao.Exist(ctx, req.ProviderId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &ai_provider_api.AdminRemoveLlmProviderResponse{
			Code: ai_provider_api.AdminRemoveLlmProviderResponse_CODE_OK,
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = config_dao.LlmProviderDao.Remove(sessCtx, req.ProviderId)
		if err != nil {
			return nil, err
		}
		return &ai_provider_api.AdminRemoveLlmProviderResponse{
			Code: ai_provider_api.AdminRemoveLlmProviderResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*ai_provider_api.AdminRemoveLlmProviderResponse), nil
}
